//
//  DefaultViewController.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/5/27.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "DefaultViewController.h"
#import "RequestManager.h"
#import "Status.h"
#import "AppDelegate.h"

#import "StatusListCollectionViewCell.h"

@interface DefaultViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>


@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation DefaultViewController


- (void)requestData {
    
    NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"];
    
    NSString *url = [NSString stringWithFormat:@"https://api.weibo.com/2/statuses/friends_timeline.json?access_token=%@", accessToken];
    [RequestManager requestWithUrl:url requestType:RequestTypeGET parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *statuses = dic[@"statuses"];
        
        for (NSDictionary *statusDic in statuses) {
            Status *status = [[Status alloc] init];
            [status setValuesForKeysWithDictionary:statusDic];
            [self.dataSource addObject:status];
        }
        
        [self.collectionView reloadData];
    } error:^(NSError *error) {
        
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[StatusListCollectionViewCell class] forCellWithReuseIdentifier:@"statusListCollectionViewCell"];
    
    
    self.dataSource = [NSMutableArray array];
    [self requestData];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    StatusListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"statusListCollectionViewCell" forIndexPath:indexPath];
    cell.status = _dataSource[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(kWidth, 300);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
